import { message } from 'antd';
import createHistory from 'history/createHashHistory';
import * as Api from './service';

const history = createHistory();

export default {
  namespace: 'login',
  state: {
    merchantInfo: {},
  },
  reducers: {
    updateMerchantInfo(state, { payload: resData }) {
      return { ...state, merchantInfo: resData };
    },
  },
  effects: {
    *doLogin({ payload }, { call, put }) {
      const { data } = yield call(Api.doLogin, payload);
      if (data.rspCd == '00000') {
        yield put({ type: 'updateMerchantInfo', payload: data.data || [] });
        localStorage.setItem('data', JSON.stringify(data.data));
        message.success('登录成功');
        history.push({
          pathname: '/order/merchant',
        });
      } else {
        message.error(data.rspInf || '登录失败');
      }
    },
  },
};
